package com.butterfly.hotkey.dashboard.framework.netty.handler;

import cn.hutool.core.collection.CollUtil;
import com.butterfly.hotkey.common.disruptor.publish.QueueEventPublisher;
import com.butterfly.hotkey.common.factory.CommonFactoryBean;
import com.butterfly.hotkey.common.netty.INettyMsgHandler;
import com.butterfly.hotkey.core.model.HotKeyModel;
import com.butterfly.hotkey.core.model.msg.HotKeyMsg;
import io.netty.channel.ChannelHandlerContext;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Collection;

import static com.butterfly.hotkey.dashboard.constant.FactoryConstant.HANDLER;
import static com.butterfly.hotkey.core.constant.MsgTypeConst.REQUEST_HIT_COUNT;

/**
 * 处理client->worker端发送的所有key数量统计
 *
 * @author butterfly
 */
@Component
@CommonFactoryBean(type = HANDLER, valueInt = REQUEST_HIT_COUNT)
public class KeyCountHandler implements INettyMsgHandler {

    @Resource
    private QueueEventPublisher queueEventPublisher;

    @Override
    public void handle(HotKeyMsg message, ChannelHandlerContext context) {
        Collection<HotKeyModel> hotKeyModels = message.getHotKeyModels();
        if (CollUtil.isEmpty(hotKeyModels)) {
            return;
        }

        // TODO  将收到的key放入延时队列，15秒后进行累加并发送，原始逻辑是暂存一段时间，批量发送给config-center
        // 但这样会受config-center的存储限制
        // 修改为直接worker推送，并增加限流
    }
}
